-module(test_server).
-export([start/0]).

start() ->
    {ok, Listen} = gen_tcp:listen(4567, [binary, {packet, 4}, 
					 {reuseaddr, true},
					 {active, false}]),
    spawn(fun() -> accept(Listen) end).

accept(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    spawn(fun() -> accept(Listen) end),
    loop(Socket).

loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
	{ok, Data} ->
	    io:format("(~p)received: ~p~n", [Socket, Data]),
	    gen_tcp:send(Socket, iolist_to_binary(["server#", Data])),
	    loop(Socket);
	{error, closed} ->
	    io:format("[~p] tcp_closed~n", [Socket])
    end.
    
